/**
 * 路由配置
 */
import Vue from 'vue'
import VueRouter from "vue-router";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {notification} from "ant-design-vue";
import tool from "@/utils/tool";

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

// 路由配置
const routes = [
    {
        path: "/",
        component: () => import('@/views/index'),
        children: [
        ]
    },
    {
        path: '*',
        redirect: "/"
    }
]

const router = new VueRouter({
    base: process.env.BASE_URL,
    routes
});

// 进度条配置
NProgress.configure({ showSpinner: false, speed: 500 })

router.beforeEach((to, from, next) => {

    NProgress.start();

    const token = tool.data.get("TOKEN");
    if (to.path === '/login') {
        if (token) {
            next({ path: '/' })
            return false;
        }
        next();
        return false;
    } else {
        tool.data.set("LAST_VIEWS_PATH", to.fullPath)
    }
    if (!token) {
        next({path: "login"})
        return false;
    }

    next();
})

router.afterEach(() => {
    NProgress.done()
})

router.onError((error) => {
    NProgress.done()
    notification.error({
        message: '路由错误',
        description: error.message
    })
})

export default router;
